<template>
  <div>
    <div  class="main_div">
      <div class="title">
        <span>账号绑定</span>
      </div>
      <el-divider></el-divider>
      <div>
        <div class="itemBox">
          <div class="left">
            <i class="el-icon-message"></i>
          </div>
          <div class="center">
            <p><span class="font1">邮箱</span>
              <span class="font3">
              </span>
              <span class="font4" v-if="user.email!=null">
                {{user.email}}
              </span>
              <span class="font4" v-else>未绑定
              </span>
            </p>
            <p class="font2">可用邮箱加密码登录慕课网，可用邮箱找回密码</p>
          </div>
          <div class="right">
            <el-button round v-if="user.email==null" @click="bindIngEmail(1)">立即绑定</el-button>
            <el-button round v-else @click="update('email')">更改</el-button>
          </div>
        </div>
        <div class="itemBox">
          <div class="left">
            <i class="el-icon-mobile-phone"></i>
          </div>
          <div class="center">
            <p><span class="font1">手机</span>
              <span class="font3">
              </span>
              <span class="font4" v-if="user.phone!=null">
                {{user.phone}}
              </span>
              <span class="font4" v-else>未绑定
              </span>
            </p>
            <p class="font2">可用手机号加密码登录慕课网，可通过手机号找回密码</p>
          </div>
          <div class="right">
            <el-button round v-if="user.phone==null" @click="bindIngPhone(1)">立即绑定</el-button>
            <el-button round v-else @click="update('phone')">更改</el-button>
          </div>
        </div>
        <div class="itemBox">
          <div class="left">
            <i class="el-icon-lock"></i>
          </div>
          <div class="center">
            <p><span class="font1">密码</span>
              <span class="font3">已设置
              </span>
            </p>
            <p class="font2">用于保护账号信息和登录安全</p>
          </div>
          <div class="right">
            <el-button round @click="dialogPasswordUpdate = true">修改</el-button>
          </div>
        </div>
      </div>
      <div>
        <!--绑定邮箱-->
        <el-dialog :title="linTitle" :visible.sync="dialogEmailBinding">
          <el-form :model="userData"
                   label-width="60px"
                   :rules="email_binding_rules"
                   ref="email_form"
          >
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="userData.email"
                        autocomplete="off"
                        class="inputDiv"
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogEmailBinding = false">取 消</el-button>
            <el-button type="primary" @click="emailBinding">确 定</el-button>
          </div>
        </el-dialog>
        <!--绑定手机号-->
        <el-dialog :title="linTitle" :visible.sync="dialogPhoneBinding">
          <el-form :model="userData"
                   label-width="80px"
                   :rules="phone_binding_rules"
                   v-loading="phoneLoading"
                   ref="phone_form"
          >
            <el-form-item label="手机号" :label-width="formLabelWidth" prop="phone">
              <el-input v-model="userData.phone"
                        autocomplete="off"
                        class="inputDiv"
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogPhoneBinding = false">取 消</el-button>
            <el-button type="primary" @click="phoneBinding">确 定</el-button>
          </div>
        </el-dialog>
        <!--修改密码-->
        <el-dialog title="修改密码" :visible.sync="dialogPasswordUpdate">
          <el-form :model="userData"
                   :label-width="formLabelWidth"
                   :rules="password_rules"
                   v-loading="passLoading"
                   ref="password_form"
          >
            <el-form-item label="原始密码" prop="oldPassword">
              <el-input v-model="userData.oldPassword"
                        type="password"
                        autocomplete="off"
                        class="inputDiv"
              ></el-input>
            </el-form-item>
            <el-form-item label="输入新密码" prop="newPassword">
              <el-input v-model="userData.newPassword"
                        type="password"
                        autocomplete="off"
                        class="inputDiv"
              ></el-input>
            </el-form-item>
            <el-form-item label="确认新密码" prop="repeatPassword">
              <el-input v-model="userData.repeatPassword"
                        type="password"
                        autocomplete="off"
                        class="inputDiv"
              >
              </el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogPasswordUpdate = false">取 消</el-button>
            <el-button type="primary" @click="passwordUpdate">确 定</el-button>
          </div>
        </el-dialog>
        <!--验证身份-->
        <el-dialog title="验证身份" :visible.sync="dialogVeri" >
          <div style="font-size: 50px">
            <i class="el-icon-key"></i>
          </div>
          <p>
            请输入登录密码验证身份
          </p>
          <span>
            {{linText}}
          </span>
          <el-form :model="userData"
                   style="padding-top: 30px"
                   :rules="password_veri_rules"
                   v-loading="veriLoading"
                   ref="veri_form">
            <el-form-item label="密码"
                          label-width="50px"
                          prop="oldPassword">
              <el-input v-model="userData.oldPassword"
                        autocomplete="off"
                        class="inputDiv"
                        type="password"
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogVeri = false">取 消</el-button>
            <el-button type="primary" @click="veriPassowrd">确 定</el-button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import {mapState} from 'vuex'

export default {
  name: 'UserBinding',
  data () {
    var validatePass2 = (rule, value, callback) => {
      if (value !== this.userData.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      formLabelWidth: '100px',
      dialogEmailBinding: false,
      dialogPhoneBinding: false,
      dialogPasswordUpdate: false,
      dialogVeri: false,
      veriLoading: false,
      emailLoading: false,
      phoneLoading: false,
      passLoading: false,
      next: '',
      // 验证密码显示字段
      linText: '',
      linTitle: '',
      linTrue: false,
      userData: {
        userId: '',
        email: '',
        phone: '',
        oldPassword: '',
        newPassword: '',
        repeatPassword: '',
        type: ''
      },
      email_binding_rules: {
        email: [
          {required: true, message: '请输入邮箱地址', trigger: 'blur'},
          {type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur']}
        ]
      },
      phone_binding_rules: {
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'}
        ]
      },
      password_veri_rules: {
        oldPassword: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ]
      },
      password_rules: {
        oldPassword: [
          {required: true, message: '请输入旧密码', trigger: 'blur'}
        ],
        newPassword: [
          {required: true, message: '请输入新密码', trigger: 'blur'}
        ],
        repeatPassword: [
          {required: true, message: '请再次输入新密码', trigger: 'blur'},
          {validator: validatePass2, trigger: 'blur'}
        ]
      }
    }
  },
  computed: mapState({
    user: 'user'
  }),
  mounted: function () {
    this.userData.userId = this.user.userId
  },
  methods: {
    bindIngEmail (type) {
      // eslint-disable-next-line eqeqeq
      if (type == 1) {
        this.linTitle = '绑定邮箱'
        // eslint-disable-next-line eqeqeq
      } else if (type == 2) {
        this.linTitle = '更改邮箱'
      }
      this.dialogEmailBinding = true
    },
    bindIngPhone (type) {
      // eslint-disable-next-line eqeqeq
      if (type == 1) {
        this.linTitle = '绑定手机号'
        // eslint-disable-next-line eqeqeq
      } else if (type == 2) {
        this.linTitle = '更改手机号'
      }
      this.dialogPhoneBinding = true
    },
    // 绑定邮箱提交
    emailBinding () {
      this.userData.type = 1
      var _this = this
      this.$refs['email_form'].validate((valid) => {
        if (valid) {
          _this.emailLoading = true
          _this.$axios.post('/api/user/userBinding',
            _this.$qs.stringify({
              userData: JSON.stringify(_this.userData)
            }))
            .then(function (response) {
              _this.emailLoading = false
              var result = response.data
              if (result.isSuccess) {
                _this.$store.commit('setEmail', result.result)
                _this.$message({
                  message: '绑定成功!',
                  type: 'success'
                })
                _this.dialogEmailBinding = false
              } else {
                _this.$message({
                  message: result.msg,
                  type: 'warning'
                })
              }
            })
            .catch(function () {
              _this.emailLoading = false
              _this.$message({
                message: '系统出错，请重新尝试',
                type: 'warning'
              })
            })
        } else {
          return false
        }
      })
      this.userData.email = ''
    },
    // 绑定手机号提交
    phoneBinding () {
      // type=2 更新手机号
      this.userData.type = 2
      var _this = this
      this.$refs['phone_form'].validate((valid) => {
        if (valid) {
          _this.phoneLoading = true
          _this.$axios.post('/api/user/userBinding',
            _this.$qs.stringify({
              userData: JSON.stringify(_this.userData)
            }))
            .then(function (response) {
              _this.phoneLoading = false
              var result = response.data
              if (result.isSuccess) {
                _this.$store.commit('setPhone', result.result)
                _this.$message({
                  message: '绑定成功!',
                  type: 'success'
                })
                _this.dialogPhoneBinding = false
              } else {
                _this.$message({
                  message: result.msg,
                  type: 'warning'
                })
              }
            })
            .catch(function () {
              _this.phoneLoading = false
              _this.$message({
                message: '系统出错，请重新尝试',
                type: 'warning'
              })
            })
        } else {
          return false
        }
      })
      _this.userData.phone = ''
    },
    veriPassowrd () {
      var _this = this
      this.userData.type = 4
      this.$refs['veri_form'].validate((valid) => {
        if (valid) {
          this.veriLoading = true
          _this.$axios.post('/api/user/userBinding',
            _this.$qs.stringify({
              userData: JSON.stringify(_this.userData)
            }))
            .then(function (response) {
              _this.veriLoading = false
              var result = response.data
              if (result.isSuccess) {
                _this.dialogVeri = false
                // eslint-disable-next-line eqeqeq
                if (_this.next == 'email') {
                  _this.bindIngEmail(2)
                } else {
                  _this.bindIngPhone(2)
                }
              } else {
                _this.$message({
                  message: result.msg,
                  type: 'warning'
                })
              }
            })
            .catch(function () {
              _this.veriLoading = false
              _this.$message({
                message: '查询出错，请重新尝试',
                type: 'warning'
              })
            })
        } else {
          return false
        }
      })
      this.userData.oldPassword = ''
    },
    // 更新密码
    passwordUpdate () {
      var _this = this
      this.userData.type = 3
      this.$refs['password_form'].validate((valid) => {
        if (valid) {
          this.passLoading = true
          _this.$axios.post('/api/user/userBinding',
            _this.$qs.stringify({
              userData: JSON.stringify(_this.userData)
            }))
            .then(function (response) {
              _this.passLoading = false
              var result = response.data
              if (result.isSuccess) {
                _this.dialogPasswordUpdate = false
                _this.$message({
                  message: '更新成功',
                  type: 'success'
                })
              } else {
                _this.$message({
                  message: result.msg,
                  type: 'warning'
                })
              }
            })
            .catch(function () {
              _this.passLoading = false
              _this.$message({
                message: '查询出错，请重新尝试',
                type: 'warning'
              })
            })
          this.userData.oldPassword = ''
          this.userData.repeatPassword = ''
          this.userData.newPassword = ''
        } else {
          return false
        }
      })
    },
    update (str) {
      this.dialogVeri = true
      // eslint-disable-next-line eqeqeq
      if (str == 'email') {
        this.linText = this.user.email
        this.next = 'email'
      }
      // eslint-disable-next-line eqeqeq
      if (str == 'phone') {
        this.linText = this.user.phone
        this.next = 'phone'
      }
    }
  }
}
</script>

<style>
  .main_div {
    margin-top: 20px;
  }
  .title {
    text-align: left;
    padding-left: 20px;
    font-size: 20px;
    font-weight: bolder;
  }
  .main_div .el-divider--horizontal{
    margin: 12px 0px;
  }
  .itemBox {
    height: 88px;
    overflow: hidden;
    margin: auto;
    border-bottom: 1px solid #d9dde1;
  }
  .left {
    width: 60px;
    float: left;
    font-size: 36px;
    color: #d9dde1;
    line-height: 98px;
    padding:0px  24px;
  }
  .center{
    width: 510px;
    float: left;
    text-align: left;
  }
  .right{
    float: right;
    padding-right: 12px;
    margin-top: 28px;
  }
  .font1 {
    color: #2b333b;
    font-size: 16px;
    font-weight: 700;
  }
  .font4 {
    color: #f01414;
    font-size: 14px;
  }
  .font2{
    color: #93999f;
    font-size: 14px;
  }
  .font3{
    font-size: 14px;
  }
  .inputDiv {
    width: 100%;
  }
  .el-dialog {
    width: 30%;
  }
</style>
